package com.example.taobaounion.view.fragment;

import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.taobaounion.R;
import com.example.taobaounion.base.BaseFragment;
import com.example.taobaounion.manager.PresenterManager;
import com.example.taobaounion.model.domain.OnSellContent;
import com.example.taobaounion.presenter.impl.OnSellPagePresenter;
import com.example.taobaounion.utils.SizeUtils;
import com.example.taobaounion.utils.TicketUtil;
import com.example.taobaounion.utils.ToastUtils;
import com.example.taobaounion.view.adapter.OnSellContentAdapter;
import com.example.taobaounion.view.callback.IOnSellPageCallback;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.util.Objects;

import butterknife.BindView;

public class OnSellFragment extends BaseFragment {

    private static final int DEFAULT_SPAN = 2;

    @BindView(R.id.on_sell_content_list)
    public RecyclerView mContentRv;

    @BindView(R.id.fragment_bar_title_tv)
    public TextView mBarTitleTv;

    @BindView(R.id.on_sell_refresh_layout)
    public TwinklingRefreshLayout mRefreshLayout;

    private OnSellPagePresenter mOnSellPagePresenter;
    private IOnSellPageCallback mIOnSellPageCallback;
    private OnSellContentAdapter mOnSellContentAdapter;

    @Override
    protected void initView(View view) {
        mContentRv.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                int i = SizeUtils.dip2px(Objects.requireNonNull(getContext()), 2.5f);
                outRect.top = i;
                outRect.bottom = i;
                outRect.left = i;
                outRect.right = i;
            }
        });


        mRefreshLayout.setEnableLoadmore(true);
        mRefreshLayout.setEnableRefresh(false);


        mBarTitleTv.setText(getResources().getText(R.string.text_on_sell_title));
    }

    @Override
    protected void initPresenter() {

        mOnSellPagePresenter = PresenterManager.getInstance().getOnSellPagePresenter();

        mIOnSellPageCallback = new IOnSellPageCallback() {
            @Override
            public void onNetworkError() {
                setUpState(STATE.ERROR);
            }

            @Override
            public void onLoading() {
                setUpState(STATE.LOADING);
            }

            @Override
            public void onEmpty() {
                setUpState(STATE.EMPTY);
            }

            @Override
            public void onContentLoadedSuccess(OnSellContent sellContent) {
                setUpState(STATE.SUCCESS);
                mOnSellContentAdapter.setData(sellContent);
            }

            @Override
            public void onMoreLoaded(OnSellContent sellContent) {
                mRefreshLayout.finishLoadmore();
                // 添加内容到适配器
                mOnSellContentAdapter.onMoreLoaded(sellContent);
                ToastUtils.showToast("成功加载" + sellContent.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data().size() + "条数据");
            }

            @Override
            public void onMoreLoadedError() {
                mRefreshLayout.finishLoadmore();
                ToastUtils.showToast("网络异常稍后重试");
            }

            @Override
            public void onMoreLoadedEmpty() {
                mRefreshLayout.finishLoadmore();
                ToastUtils.showToast("没有跟多的内容");
            }
        };
        mOnSellPagePresenter.registerViewCallback(mIOnSellPageCallback);
    }

    @Override
    protected void initAdapter() {
        mOnSellContentAdapter = new OnSellContentAdapter();
        mContentRv.setAdapter(mOnSellContentAdapter);
        mContentRv.setLayoutManager(new GridLayoutManager(getContext(), DEFAULT_SPAN));
    }

    @Override
    protected void initListener() {
        mOnSellContentAdapter.setOnSellPageItemClickListener(mapDataBean -> TicketUtil.toTicketPage(getContext(), mapDataBean));

        mRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                mOnSellPagePresenter.loaderMore();
            }
        });
    }

    @Override
    protected void initData() {
        mOnSellPagePresenter.getOnSellContent();
    }

    @Override
    protected int getRootViewResId() {
        return R.layout.fragment_on_sell;
    }

    @Override
    protected View loadRootView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_with_bar_layout, container, false);
    }

    @Override
    protected void release() {
        if (mOnSellPagePresenter != null) {
            mOnSellPagePresenter.unRegisterViewCallback(mIOnSellPageCallback);
        }
    }
}
